package com.xiaohua.api.common.constants;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * TextIn API 字段映射类
 * 存放各种证件类型的字段映射（英文到中文）和需要忽略的元数据字段
 * 
 * @author 小花
 * @date 2025-03-30
 */
public class TextInFieldMappings {

    /**
     * 不重要的技术元数据字段，默认不包含在结果中
     */
    public static final Set<String> IGNORE_METADATA_FIELDS = new HashSet<String>() {{
        add("image_angle");
        add("rotated_image_width");
        add("rotated_image_height");
        add("type");
        add("type_description");
        add("imageAngle");
        add("imageWidth");
        add("imageHeight");
        add("confidence");
        add("position");
        add("rotation_degree");
        add("position_top_left_x");
        add("position_top_left_y");
        add("position_top_right_x");
        add("position_top_right_y");
        add("position_bottom_left_x");
        add("position_bottom_left_y");
        add("position_bottom_right_x");
        add("position_bottom_right_y");
    }};
    
    /**
     * 通用图像字段映射
     */
    private static final Map<String, String> COMMON_IMAGE_FIELD_MAPPINGS = new HashMap<String, String>() {{
        put("image_angle", "图像角度");
        put("rotated_image_width", "图像宽度");
        put("rotated_image_height", "图像高度");
        put("image_width", "原始图像宽度");
        put("image_height", "原始图像高度");
        put("crop_image", "裁剪后图像");
        put("head_portrait", "头像图像");
        put("image", "原始图像");
    }};
    
    /**
     * 截断特定字段的值，针对API返回的大字段（如图像）
     * 
     * @param key 字段名
     * @param value 字段值
     * @return 处理后的字段值
     */
    public static Object truncateFieldValue(String key, Object value) {
        if (value == null) {
            return null;
        }
        
        // 如果是图像相关字段且值为字符串，截断显示
        if ((key.contains("image") || key.contains("portrait")) && value instanceof String) {
            String strValue = (String) value;
            if (strValue.length() > 100) {
                return strValue.substring(0, 20) + "...[截断]..." + strValue.substring(strValue.length() - 20);
            }
        }
        
        return value;
    }
    
    /**
     * 获取身份证字段映射
     * 
     * @return 身份证字段映射（英文到中文）
     */
    public static Map<String, String> getIdCardFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("name", "姓名");
        fieldMapping.put("sex", "性别");
        fieldMapping.put("nationality", "民族");
        fieldMapping.put("birth", "出生日期");
        fieldMapping.put("address", "住址");
        fieldMapping.put("id_number", "身份证号");
        fieldMapping.put("issue_authority", "签发机关");
        fieldMapping.put("validate_date", "有效期限");
        fieldMapping.put("is_gray", "是否为黑白复印件");
        fieldMapping.put("id_number_image", "身份证号码图像");
        fieldMapping.put("head_portrait", "头像");
        fieldMapping.put("crop_image", "切边图像");
        fieldMapping.put("is_forge", "证件真伪");
        fieldMapping.put("confidence", "置信度");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        // 添加更多可能的字段
        fieldMapping.put("type", "证件类型");
        fieldMapping.put("type_description", "证件类型描述");
        fieldMapping.put("image_angle", "图像角度");
        fieldMapping.put("rotated_image_width", "图像宽度");
        fieldMapping.put("rotated_image_height", "图像高度");
        fieldMapping.put("description", "描述");
        fieldMapping.put("position", "位置");
        fieldMapping.put("value", "值");
        
        return fieldMapping;
    }
    
    /**
     * 获取银行卡字段映射
     * 
     * @return 银行卡字段映射（英文到中文）
     */
    public static Map<String, String> getBankCardFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        fieldMapping.put("card_number", "卡号");
        fieldMapping.put("card_type", "卡片类型");
        fieldMapping.put("bank_name", "银行名称");
        fieldMapping.put("bank_id", "银行ID");
        fieldMapping.put("expiry_date", "有效期");
        
        // 添加TextIn API文档中的字段
        fieldMapping.put("issuer", "发卡机构");
        fieldMapping.put("issuer_id", "发卡机构代码");
        fieldMapping.put("validate", "有效期");
        fieldMapping.put("holder_name", "持卡人姓名");
        fieldMapping.put("card_number_verification", "卡号校验");
        fieldMapping.put("type", "银行卡类型");
        fieldMapping.put("crop_image", "裁剪后图像");
        
        // 添加可能出现的额外字段
        fieldMapping.put("card_organization", "发卡组织");
        fieldMapping.put("card_category", "卡片类别");
        fieldMapping.put("issue_bank", "发卡银行");
        fieldMapping.put("card_holder_name", "持卡人姓名");
        fieldMapping.put("cvv", "安全码");
        fieldMapping.put("issuing_city", "发卡城市");
        fieldMapping.put("card_bin", "卡bin");
        fieldMapping.put("card_issuer", "发卡机构");
        fieldMapping.put("valid_date", "有效日期");
        fieldMapping.put("expired_date", "过期日期");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取营业执照字段映射
     * 
     * @return 营业执照字段映射（英文到中文）
     */
    public static Map<String, String> getBusinessLicenseFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("BizLicenseIsCopy", "是否为副本");
        fieldMapping.put("BizLicenseIsElectronic", "是否为电子营业执照");
        fieldMapping.put("BizLicenseCreditCode", "统一社会信用代码");
        fieldMapping.put("BizLicenseRegistrationCode", "注册号");
        fieldMapping.put("BizLicenseSerialNumber", "证照编号");
        fieldMapping.put("BizLicenseCompanyName", "企业名称");
        fieldMapping.put("BizLicenseCompanyType", "企业类型");
        fieldMapping.put("BizLicenseAddress", "经营场所");
        fieldMapping.put("BizLicenseComposingForm", "组成形式");
        fieldMapping.put("BizLicenseOwnerName", "经营者");
        fieldMapping.put("BizLicenseRegCapital", "注册资本");
        fieldMapping.put("BizLicensePaidInCapital", "实收资本");
        fieldMapping.put("BizLicenseStartTime", "注册日期");
        fieldMapping.put("BizLicenseOperatingPeriod", "营业期限");
        fieldMapping.put("BizLicenseScope", "经营范围");
        fieldMapping.put("BizLicenseRegistrationDate", "登记日期");
        fieldMapping.put("BizLicenseRegistrationAuthority", "登记机关");
        
        // 保留原有字段映射
        fieldMapping.put("company_name", "企业名称");
        fieldMapping.put("credit_code", "统一社会信用代码");
        fieldMapping.put("type_of_organization", "组织形式");
        fieldMapping.put("business_address", "企业地址");
        fieldMapping.put("legal_representative", "法定代表人");
        fieldMapping.put("registered_capital", "注册资本");
        fieldMapping.put("founding_date", "成立日期");
        fieldMapping.put("business_validity", "营业期限");
        fieldMapping.put("business_scope", "经营范围");
        fieldMapping.put("registration_authority", "登记机关");
        fieldMapping.put("approval_date", "核准日期");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取行驶证字段映射
     * 
     * @return 行驶证字段映射（英文到中文）
     */
    public static Map<String, String> getVehicleLicenseFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("plate_no", "号牌号码");
        fieldMapping.put("vehicle_type", "车辆类型");
        fieldMapping.put("owner", "所有人");
        fieldMapping.put("address", "住址");
        fieldMapping.put("use_character", "使用性质");
        fieldMapping.put("model", "品牌型号");
        fieldMapping.put("vin", "车辆识别代码");
        fieldMapping.put("engine_no", "发动机号码");
        fieldMapping.put("register_date", "注册日期");
        fieldMapping.put("issue_date", "发证日期");
        fieldMapping.put("plate_no_second", "号牌号码(副页)");
        fieldMapping.put("file_number", "档案编号");
        fieldMapping.put("person_number", "核定载人数");
        fieldMapping.put("total_quality", "总质量");
        fieldMapping.put("curing_quality", "整备质量");
        fieldMapping.put("load_quality", "核定载质量");
        fieldMapping.put("size", "外廓尺寸");
        fieldMapping.put("tow_quality", "准牵引总质量");
        fieldMapping.put("remarks", "备注");
        fieldMapping.put("test_valid", "检验有效期");
        fieldMapping.put("test_record", "检验记录");
        fieldMapping.put("code", "副页编码");
        
        // 保留原有字段映射
        fieldMapping.put("plate_number", "号牌号码");
        fieldMapping.put("usage", "使用性质");
        fieldMapping.put("brand", "品牌型号");
        fieldMapping.put("identification_code", "车辆识别代号");
        fieldMapping.put("engine_number", "发动机号码");
        fieldMapping.put("registered_date", "注册日期");
        fieldMapping.put("issuing_date", "发证日期");
        
        // 添加更多可能出现的字段
        fieldMapping.put("vehicle_model", "车辆型号");
        fieldMapping.put("color", "车身颜色");
        fieldMapping.put("fuel_type", "燃料种类");
        fieldMapping.put("manage_code", "管理代码");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取驾驶证字段映射
     * 
     * @return 驾驶证字段映射（英文到中文）
     */
    public static Map<String, String> getDriverLicenseFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        fieldMapping.put("license_number", "证号");
        fieldMapping.put("name", "姓名");
        fieldMapping.put("sex", "性别");
        fieldMapping.put("nationality", "国籍");
        fieldMapping.put("address", "住址");
        fieldMapping.put("birth_date", "出生日期");
        fieldMapping.put("issue_date", "初次领证日期");
        fieldMapping.put("class", "准驾车型");
        fieldMapping.put("valid_date", "有效期限");
        
        // 添加更多可能出现的字段
        fieldMapping.put("issuing_authority", "发证机关");
        fieldMapping.put("archive_number", "档案编号");
        fieldMapping.put("record", "记录");
        fieldMapping.put("restriction", "驾驶证限制");
        fieldMapping.put("record_start_date", "记录起始日期");
        fieldMapping.put("record_end_date", "记录截止日期");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取护照字段映射
     * 
     * @return 护照字段映射（英文到中文）
     */
    public static Map<String, String> getPassportFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("passport_number", "护照号码");
        fieldMapping.put("country_code", "国家码");
        fieldMapping.put("sex", "性别");
        fieldMapping.put("pinyin", "拼音");
        fieldMapping.put("name", "姓名");
        fieldMapping.put("birthday", "出生日期");
        fieldMapping.put("validity", "有效期");
        fieldMapping.put("passport_nation", "国籍");
        fieldMapping.put("issue_date", "签发日期");
        fieldMapping.put("birthplace", "出生地点");
        fieldMapping.put("issue_place", "签发地点");
        fieldMapping.put("passport_line1", "护照下方第一行");
        fieldMapping.put("passport_line2", "护照下方第二行");
        fieldMapping.put("issued_by", "签发机关");
        fieldMapping.put("surname", "姓");
        fieldMapping.put("given_name", "名");
        fieldMapping.put("crop_image", "证件图像");
        fieldMapping.put("head_portrait", "头像");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取社保卡字段映射
     * 
     * @return 社保卡字段映射（英文到中文）
     */
    public static Map<String, String> getSocialSecurityCardFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("name", "姓名");
        fieldMapping.put("social_security_card_number", "号码(芯片下方)");
        fieldMapping.put("card_number", "卡号");
        fieldMapping.put("issue_date", "发卡日期");
        fieldMapping.put("social_security_number", "社会保障号码");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取户口本字段映射
     * 
     * @return 户口本字段映射（英文到中文）
     */
    public static Map<String, String> getHouseholdRegisterFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("member_name", "姓名");
        fieldMapping.put("member_householder_or_relationship_with_householder", "户主或与户主关系");
        fieldMapping.put("member_used_name", "曾用名");
        fieldMapping.put("member_sex", "性别");
        fieldMapping.put("member_birthplace", "出生地");
        fieldMapping.put("member_nationality", "民族");
        fieldMapping.put("member_origin_place", "籍贯");
        fieldMapping.put("member_birth", "出生日期");
        fieldMapping.put("member_other_address_this_city", "本市(县)其他住址");
        fieldMapping.put("member_religion", "宗教信仰");
        fieldMapping.put("member_id_number", "身份证号码");
        fieldMapping.put("member_height", "身高");
        fieldMapping.put("member_blood_type", "血型");
        fieldMapping.put("member_education_background", "文化程度");
        fieldMapping.put("member_marital_status", "婚姻状况");
        fieldMapping.put("member_military_service_status", "兵役状况");
        fieldMapping.put("member_unit_of_service", "服务处所");
        fieldMapping.put("member_profession", "职业");
        fieldMapping.put("member_when_and_where_to_move_to_this_city", "何时由何地迁来本市(县)");
        fieldMapping.put("member_when_and_where_to_move_to_this_site", "何时由何地迁来本址");
        fieldMapping.put("member_registration_date", "登记日期");
        fieldMapping.put("household_residence_type", "户别");
        fieldMapping.put("household_householder_name", "户主姓名");
        fieldMapping.put("household_address", "住址");
        fieldMapping.put("household_residence_number", "户号");
        fieldMapping.put("household_date", "签发日期");
        fieldMapping.put("subtype", "子类型");

        // 保留原有字段映射
        fieldMapping.put("household_address", "户籍地址");
        fieldMapping.put("household_type", "户籍类别");
        fieldMapping.put("household_id", "户号");
        fieldMapping.put("name", "姓名");
        fieldMapping.put("relationship", "户主或与户主关系");
        fieldMapping.put("sex", "性别");
        fieldMapping.put("birth_date", "出生日期");
        fieldMapping.put("id_number", "身份证号码");
        fieldMapping.put("ethnicity", "民族");
        fieldMapping.put("native_place", "籍贯");
        fieldMapping.put("birth_place", "出生地");
        fieldMapping.put("registration_date", "户口登记日期");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取结婚证字段映射
     * 
     * @return 结婚证字段映射（英文到中文）
     */
    public static Map<String, String> getMarriageCertificateFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("marriage_holder", "持证人");
        fieldMapping.put("marriage_registration_time", "登记日期");
        fieldMapping.put("marriage_num", "结婚证字号");
        fieldMapping.put("remarks", "备注");
        fieldMapping.put("marriage_name", "姓名1");
        fieldMapping.put("marriage_gender", "性别1");
        fieldMapping.put("marriage_nationality", "国籍1");
        fieldMapping.put("marriage_date_of_birth", "出生日期1");
        fieldMapping.put("marriage_id_num", "身份证号1");
        fieldMapping.put("marriage_name_2", "姓名2");
        fieldMapping.put("marriage_gender_2", "性别2");
        fieldMapping.put("marriage_nationality_2", "国籍2");
        fieldMapping.put("marriage_date_of_birth_2", "出生日期2");
        fieldMapping.put("marriage_id_num_2", "身份证号2");
        
        // 保留原有字段映射
        fieldMapping.put("marriage_certificate_number", "结婚证号码");
        fieldMapping.put("marriage_date", "结婚日期");
        fieldMapping.put("issue_date", "签发日期");
        fieldMapping.put("registration_authority", "登记机关");
        fieldMapping.put("husband_name", "丈夫姓名");
        fieldMapping.put("husband_birth_date", "丈夫出生日期");
        fieldMapping.put("husband_id", "丈夫身份证号");
        fieldMapping.put("wife_name", "妻子姓名");
        fieldMapping.put("wife_birth_date", "妻子出生日期");
        fieldMapping.put("wife_id", "妻子身份证号");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取房产证字段映射
     * 
     * @return 房产证字段映射（英文到中文）
     */
    public static Map<String, String> getPropertyCertificateFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        fieldMapping.put("ownership_number", "权属编号");
        fieldMapping.put("building_number", "幢号");
        fieldMapping.put("locate_location", "房地坐落");
        fieldMapping.put("land_plots_code", "宗地号");
        fieldMapping.put("house_structure", "结构");
        fieldMapping.put("house_total_floors", "总层数");
        fieldMapping.put("house_owner", "房屋所有权人");
        fieldMapping.put("period_of_use", "使用年限");
        fieldMapping.put("house_layers", "层数");
        fieldMapping.put("construction_area", "建筑面积");
        fieldMapping.put("ownership_nature", "所有权性质");
        fieldMapping.put("use", "用途");
        fieldMapping.put("total_condition", "共有情况");
        fieldMapping.put("registration_date", "登记日期");
        fieldMapping.put("right_to_use", "使用权取得方式");
        fieldMapping.put("oblige", "权利人");
        fieldMapping.put("attribute_quality", "权属性质");
        fieldMapping.put("usage_type", "使用权类型");
        fieldMapping.put("land_use", "土地用途");
        fieldMapping.put("expiry_date", "终止日期");
        fieldMapping.put("cover_area", "套内建筑面积");
        fieldMapping.put("building_area", "合计建筑面积");
        fieldMapping.put("type", "证件类型");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取出生证明字段映射
     * 
     * @return 出生证明字段映射（英文到中文）
     */
    public static Map<String, String> getBirthCertificateFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("baby_name", "新生儿姓名");
        fieldMapping.put("baby_gender", "新生儿性别");
        fieldMapping.put("baby_birth", "出生时间");
        fieldMapping.put("gestational_age", "出生孕周");
        fieldMapping.put("birth_weight", "出生体重");
        fieldMapping.put("birth_length", "出生身长/cm");
        fieldMapping.put("birth_place", "出生地点");
        fieldMapping.put("medical_institutions", "医疗机构名称");
        fieldMapping.put("mother_name", "母亲姓名");
        fieldMapping.put("mother_age", "母亲年龄");
        fieldMapping.put("mother_nation", "母亲国籍");
        fieldMapping.put("mother_ethnic", "母亲民族");
        fieldMapping.put("mother_address", "母亲住址");
        fieldMapping.put("mother_certificate_type", "母亲有效身份证件类别");
        fieldMapping.put("mother_certificate_id", "母亲有效身份证件号码");
        fieldMapping.put("father_name", "父亲姓名");
        fieldMapping.put("father_age", "父亲年龄");
        fieldMapping.put("father_nation", "父亲国籍");
        fieldMapping.put("father_ethnic", "父亲民族");
        fieldMapping.put("father_address", "父亲住址");
        fieldMapping.put("father_certificate_type", "父亲有效身份证件类别");
        fieldMapping.put("father_certificate_id", "父亲有效身份证件号码");
        fieldMapping.put("issued_authority", "签发机构");
        fieldMapping.put("issued_date", "签发日期");
        fieldMapping.put("birth_certificate_number", "出生医学证明编号");
        
        // 保留原有字段映射
        fieldMapping.put("name", "新生儿姓名");
        fieldMapping.put("sex", "新生儿性别");
        fieldMapping.put("birth_date", "出生日期");
        fieldMapping.put("birth_hospital", "接生机构");
        fieldMapping.put("issuing_hospital", "签发机构");
        fieldMapping.put("issue_date", "签发日期");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取学位证字段映射
     * 
     * @return 学位证字段映射（英文到中文）
     */
    public static Map<String, String> getDegreeCertificateFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        // 根据TextIn API文档添加字段
        fieldMapping.put("type", "类型");
        fieldMapping.put("name", "姓名");
        fieldMapping.put("sex", "性别");
        fieldMapping.put("born", "出生");
        fieldMapping.put("school", "院校");
        fieldMapping.put("professional", "专业");
        fieldMapping.put("degree", "学位");
        fieldMapping.put("certificate_number", "证书编号");
        fieldMapping.put("issue_date", "颁发日期");
        
        // 保留原有字段映射
        fieldMapping.put("degree_certificate_number", "学位证书编号");
        fieldMapping.put("graduate_school", "毕业学校");
        fieldMapping.put("major", "专业");
        fieldMapping.put("education_level", "学历");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
    
    /**
     * 获取毕业证字段映射
     * 
     * @return 毕业证字段映射（英文到中文）
     */
    public static Map<String, String> getGraduationCertificateFieldMappings() {
        Map<String, String> fieldMapping = new HashMap<>();
        fieldMapping.put("graduation_certificate_number", "毕业证书编号");
        fieldMapping.put("name", "姓名");
        fieldMapping.put("sex", "性别");
        fieldMapping.put("graduate_school", "毕业学校");
        fieldMapping.put("major", "专业");
        fieldMapping.put("education_level", "学历");
        fieldMapping.put("graduation_date", "毕业日期");
        fieldMapping.put("study_duration", "学习年限");
        fieldMapping.put("issue_date", "发证日期");
        
        // 添加图像相关字段的中文映射
        fieldMapping.putAll(COMMON_IMAGE_FIELD_MAPPINGS);
        
        return fieldMapping;
    }
} 